﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class ProjectBUS
    {
        public ResponseMessage AddProject(ProjectInfo project, out int Isdefault)
        {
            ResponseMessage message = new ResponseMessage();
            Isdefault = 0;
            ProjectDAL da = new ProjectDAL();
            HomeDAL homeDAL = new HomeDAL();
            int exist = homeDAL.GetDefaultProject();
            if (exist > 0)
            {
                project.Isdefault = 0;
            }
            else
            {
                project.Isdefault = 1;
            }
            bool result = da.AddProject(project);
            if (result)
            {
                Isdefault = project.Isdefault;
                message.errcode = 0;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "新增项目信息失败";
            }
            return message;
        }

        public ResponseMessage EditProject(ProjectInfo project)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            bool result = da.EditProject(project);
            if (result)
            {
                message.errcode = 0;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "修改项目信息失败";
            }
            return message;
        }

        public ResponseMessage DeleteProject(ProjectInfo project)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            bool result = da.DeleteProject(project.ID);
            if (result)
            {
                message.errcode = 0;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "删除项目信息失败";
            }
            return message;
        }

        public ResponseMessage QueryProject(ProjectInfo project, string username, int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            if (username != "fstar")
            {
                project.ID = projectid;
            }
            List<ProjectInfo> list = da.QueryProject(username, project);
            message.errcode = 0;
            message.data = list;
            message.total = da.QueryProjectCount(username, project);
            message.errmsg = "";

            return message;
        }

        public ResponseMessage LoadProject(ProjectInfo project)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            ProjectInfo info = da.LoadProject(project.ID);
            message.errcode = 0;
            message.data = info;
            message.errmsg = "";

            return message;
        }
        public ResponseMessage QueryApplyType()
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            List<ApplyType> list = da.QueryApplyType();
            message.errcode = 0;
            message.data = list;

            return message;
        }

        public ResponseMessage BindMenu(string menurole, int proid)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            bool result = da.BindMenu(menurole, proid);
            if (result)
            {
                message.errcode = 0;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "绑定菜单失败";
            }
            return message;
        }

        public ResponseMessage SwitchProject(int id)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectDAL da = new ProjectDAL();
            bool result = da.SwitchProject(id);
            if (result)
            {
                message.errcode = 0;
                message.errmsg = "";
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "切换项目信息失败";
            }
            return message;
        }
    }
}
